<template>
  <el-dialog :visible.sync="dialog" title="重置密码" append-to-body width="300px">
    <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="80px">
      <el-row :gutter="24">
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" />
        </el-form-item>
      </el-row>
      <el-row :gutter="24">
        <el-form-item label="密码确认" prop="password2">
          <el-input v-model="form.password2" type="password" />
        </el-form-item>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>

import { add, edit, reset } from '@/api/user'
import { getDepts } from '@/api/dept'
import { getAll, getLevel } from '@/api/role'
import { getAllJob } from '@/api/job'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { isvalidPhone } from '@/utils/validate'

export default {
  components: { Treeselect },
  props: {
    supThis: {
      type: Object,
      default: null
    }
  },
  data() {
    const validatePassword = (rule, value, callback) => {
      console.log(value, this.form.password);
      if (!value) {
        callback(new Error('请输入密码'))
      } else if (value !== this.form.password) {
        callback(new Error('请输入一致密码'))
      } else {
        callback()
      }
    }

    return {
      dialog: false,
      loading: false,
      form: { password: '', password2: '' },
      style: 'width: 184px',
      level: 3,
      rules: {
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        password2: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      }
    }
  },
  created() {
    const explorer = navigator.userAgent
    if (explorer.indexOf('Chrome') >= 0) {
      this.style = 'width: 184px'
    } else {
      this.style = 'width: 172px'
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      const _this = this

      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.doReset();
        } else {
          return false
        }
      })
    },
    doReset() {
      const data = { id: this.form.id, password: this.form.password, username: this.form.username }
      reset(data).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = { password: '', password2: '' }
    }
  }
}
</script>

<style scoped>

</style>
